using System.Linq;
using System.Web.Mvc;
using WRSManagementSystem.DomainModel.Entities;
using WRSManagementSystem.DomainModel.Repositories;
using System.Collections.Generic;
using System;


namespace WRSManagementSystem.Controllers
{
    public class ProjectController : Controller
    {
        private readonly IPeopleRepository _peoplerepo;
        private readonly IProjectsRespoitory _projectrepo;
        //
        // GET: /Project/

        public ProjectController(IProjectsRespoitory projectrepo, IPeopleRepository peoplerepository)
        {
            _projectrepo = projectrepo;
            _peoplerepo = peoplerepository;
        }

        public ActionResult Index()
        {
            ViewData["Creators"] = GetPeopleSelectList();
            return View(_projectrepo.Find());
        }

        //
        // GET: /Project/Details/5

        public ActionResult Details(int id)
        {
            return View(_projectrepo.FindOne(p => p.Id == id));
        }

        //
        // GET: /Project/Create

        public ActionResult Create()
        {
            ViewData["Creators"] = GetPeopleSelectList();
            var project = new Project();
            project.StartDate = System.DateTime.Today;
            project.EndDate = System.DateTime.Today;
            return View(project);
        }

        //
        // POST: /Project/Create

        [HttpPost]
        public ActionResult Create(Project model, FormCollection fc)
        {
            ViewData["Creators"] = GetPeopleSelectList();
            model.Creator = _peoplerepo.FindOne(person => person.Id == model.Creator.Id);

            //Person p = null;
            //foreach (var tp in _peoplerepo.Find())
            //{
            //    p = tp;
            //    if ((tp.Name + " " + tp.Surname) == fc["Creator"])
            //        break;
            //}
            //model.Creator = p;

      
            if (ModelState.IsValid || true)
            {
                try
                {
                    // TODO: Add insert logic here
                    _projectrepo.Add(model);
                    _projectrepo.Save();
                    return RedirectToAction("Index");
                }
                catch(Exception e)
                {
                   // ViewData["Creators"] = GetPeopleSelectList();
                    return View(model);
                }
            }
            ViewData["Creators"] = GetPeopleSelectList(model.Creator);
            return View(model);
        }

        //
        // GET: /Project/Edit/5

        public ActionResult Edit(int id)
        {
            ViewData["Creators"] = GetPeopleSelectList();
            return View(_projectrepo.FindOne(p => p.Id == id));
        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        public ActionResult Edit(Project model, FormCollection fc)
        {
            ViewData["Creators"] = GetPeopleSelectList();
            //var s = (fc["StartDate"]).Split(new char[] { '/', '-' });
            //var e = fc["EndDate"].Split(new char[] { '/', '-' });
            Person p = null;
            foreach (var tp in _peoplerepo.Find())
            {
                p = tp;
                if ((tp.Name + " " + tp.Surname) == fc["Creator"])
                    break;
            }
            //model.Creator = _peoplerepo.FindOne(p => (p.Name + " " + p.Surname) == fc["Creator"]);
            model.Creator = p;

            //model.StartDate = new System.DateTime(int.Parse(s[2]), int.Parse(s[0]), int.Parse(s[1]));
            //model.EndDate = new System.DateTime(int.Parse(e[2]), int.Parse(e[0]), int.Parse(e[1]));
            if (ModelState.IsValid || true)
            {
                try
                {
                    // TODO: Add update logic here
                    _projectrepo.Update(model);
                   // _projectrepo.Save();
                    return RedirectToAction("Index");
                }
                catch(Exception e)
                {
                    //ViewData["Creators"] = GetPeopleSelectList();
                    return View(model);
                }
            }
            ViewData["Creators"] = GetPeopleSelectList();
            return View(model);
        }

        private SelectList GetPeopleSelectList(Person person)
        {
            return new SelectList(_peoplerepo.Find().Select(p => p.Name + " " + p.Surname));
        }

        private SelectList GetPeopleSelectList()
        {
            return new SelectList(_peoplerepo.Find(), "Id", "Name");
        }

        //private System.Collections.Generic.List<SelectListItem> GetPeopleSelectList()
        //{
        //    var lsli = new System.Collections.Generic.List<SelectListItem>();
        //    foreach( var p in _peoplerepo.Find())
        //        lsli.Add( new SelectListItem(){ Text = p.Name + " " + p.Surname, Value = string.Format("{0}",p.Id) } );

        //    return lsli;
        //}

        //
        // GET: /Project/Report

        public ActionResult Report()
        {
            int id = int.Parse(Request.Params["creatorsDropDownList"]);
            var a = _projectrepo.Find().Where(t => t.Creator.Id == id);
            return View(a);
        }
    }
}